package com.deepalhome.deepalclub

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.deepalhome.ImageAdapter
import com.deepalhome.deepalclub.adb.AdbConnectionManager
import com.deepalhome.deepalclub.adb.AdbUtil
import com.deepalhome.deepalclub.databinding.ActivityMainBinding
import com.deepalhome.deepalclub.databinding.ConnectionDialogBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ToastUtils.getDefaultMaker().setGravity(Gravity.CENTER, 0, 0)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        AdbUtil.setOnConnectionChangedListener { isConnected ->
            if (isConnected) {
                binding.toolbar.subtitle = "已连接车机"
            } else {
                binding.toolbar.subtitle = "未连接车机"
            }
        }
        binding.disableHealthManagerBtn.setOnClickListener {
            if (showConnectionDialog()) return@setOnClickListener
            AdbUtil.disableHealthManager()
        }
        binding.grantDeepalHomeBtn.setOnClickListener {
            if (showConnectionDialog()) return@setOnClickListener
            AdbUtil.grantDeepalHome()
        }
        binding.repairBtn.setOnClickListener {
            if (showConnectionDialog()) return@setOnClickListener
            AdbUtil.repairAppStore()
        }
    }

    private fun showConnectionDialog(): Boolean {
        if (!AdbConnectionManager.isConnected) {
            val connectionDialogBinding = ConnectionDialogBinding.inflate(LayoutInflater.from(this))
            connectionDialogBinding.devBtn.setOnClickListener {
                if (Settings.Secure.getInt(
                        this.contentResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0
                    ) != 0
                ) {
                    ActivityUtils.startActivity(Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS))
                } else {
                    ActivityUtils.startActivity(Intent(Settings.ACTION_DEVICE_INFO_SETTINGS))
                    ToastUtils.showLong("请连续点击“版本号”开启开发者模式")
                }
            }
            val dialog: AlertDialog =
                AlertDialog.Builder(this).setTitle("连接车机").setMessage("请输入IP地址和端口")
                    .setView(connectionDialogBinding.root).setNeutralButton("帮助", null)
                    .setPositiveButton("连接", null).setNegativeButton("取消", null).create()
            dialog.show()
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
                AdbUtil.connect("127.0.0.1", connectionDialogBinding.portEt) { isConnected ->
                    if (isConnected) {
                        dialog.dismiss()
                    }
                }
            }
            dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener {
                showHelpDialog()
            }
            return true
        }
        return false
    }

    private fun showHelpDialog() {
        MaterialAlertDialogBuilder(this).setTitle("帮助")
            .setView(Banner<Int, ImageAdapter>(this).apply {
                isAutoLoop(false)
                setAdapter(
                    ImageAdapter(
                        arrayListOf(
                            R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3, R.mipmap.guide_4
                        )
                    )
                ).addBannerLifecycleObserver(this@MainActivity) //添加生命周期观察者
                    .setIndicator(CircleIndicator(this@MainActivity))
            }).setPositiveButton("我知道了", null).show()
    }

    override fun onDestroy() {
        AdbUtil.destroy()
        super.onDestroy()
    }
}